import React from 'react';
import ReactDom from 'react-dom';
import { Router, hashHistory, browserHistory } from 'react-router';
import './index.scss';
import 'css/fonts.css';
import eventHandle from 'js/core/eventHandle';
import { api } from 'js/api/api';

const routes = [
    {
        path: '/init',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/index/init').default)
            }, 'init')
        },
        childRoutes: [
            {
                path: '/login',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/login/login').Login)
                    }, 'login')
                }
            },
            {
                path: '/register',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/login/login').Register)
                    }, 'register')
                }
            },
            {
                path: '/playerSelect',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/playerSelect/playerSelect').default)
                    }, 'playerSelect')
                }
            },
            {
                path: '/createPlayer',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/playerSelect/createPlayer').default)
                    }, 'createPlayer')
                }
            },
        ]
    },
    {
        path: '/battle',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/maps/maze/battle').default)
            }, 'battle')
        }
    },
    {
        path: '/bag',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/modal/bag/bag').default)
            }, 'bag')
        }
    },
    {
        path: '/main',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/main/main').default)
            }, 'main')
        },
        childRoutes: [
            {
                path: '/map',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/maps/map/map').default)
                    }, 'map')
                }
            },
            {
                path: '/maze',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/maps/maze/maze').default)
                    }, 'maze')
                }
            },
            {
                path: '/town',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/maps/town/town').default)
                    }, 'town')
                }
            },
        ]
    }
];


api('getErrMap', (err, errMapData) => {
    window.errMap = errMapData;
    api('getConfig', (err, config) => {
        window.propName = config.propName;
        window.totalConfig = config.totalConfig;
        for (let i in config.totalConfig) {
            console.log(JSON.stringify(i))
            console.log(JSON.stringify(config.totalConfig[i]))
        }

        window.totalConfigById = config.totalConfigById;
        api('selectPlayer', {}, (err, playerData) => {
            if (err === 0) {
                window.player = playerData;
                ReactDom.render(
                    <Router history={browserHistory} routes={routes} />
                    , document.getElementById('app'))
            }
        });
    });
});

